﻿using System.Text;
using HtmlAgilityPack;
using ProcessLib.Ratp.Easy.Extension;
using ProcessLib.Ratp.Easy.Select;
using ProcessLib.Ratp.Page.Region;
using StyleSharp.Styles;

namespace ProcessLib.Ratp.Easy.ParagraphCompose;

public class SpanProcessor
{
    public Parameters? Parameters
    {
        get;
        set;
    } = new()
    {
        BodyFontFamilies = new ()
        {
            "ff3"
        },
        BodyFontSizes = new ()
        {
            "fs2"
        },
        BodyFontWidth = 1750,
        DontTranslateFamilies = new ()
        {
            "ff4"
        },
        InlineWholeFontFamilies = new ()
        {
            "ff4"
        }
    };
    public string Process(List<SemanticParagraph> paragraphs, StyleNode styleNode)
    {
        var builder = new StringBuilder();
        foreach (var pa in paragraphs)
        {
            builder.Append(Process(pa,styleNode));
        }
        return builder.ToString();
    }

    public static string Raw(SemanticParagraph semanticParagraph)
    {
        var builder = new StringBuilder();
        foreach (var line in semanticParagraph.LineRegions)
        {
            builder.Append(line.Div);
        }

        return builder.ToString();
    }
    public string Process(SemanticParagraph paragraph, StyleNode styleNode)
    {
        if (!Parameters.BodyFontFamilies.Contains(paragraph.FontFamily))
        {
            return Raw(paragraph);
        }

        if (paragraph.LineRegions.Count < 1) return "";
        var firstNode = DivHelpSelector.Select(
            paragraph.LineRegions.Last().Div, "div");
        var builder = new StringBuilder();
        var document = new HtmlDocument();
        document.LoadHtml(Raw(paragraph));
        var nodes = document.DocumentNode.SelectNodes("//div[contains(@class,'t')]");
        foreach (var node in nodes)
        {
            ExtractTextFromNode(node);
        }

        firstNode.InnerHtml = builder.ToString();
        var ca = firstNode.GetAttribute("class");
        var h = Extractor.ExtractorHeight(ca);
        ca = ca.Replace(h, "");
        firstNode.Attributes.Remove("class");
        firstNode.Attributes.Add("class",ca);
        firstNode.Attributes.Add("style",$"width: {Parameters.BodyFontWidth}px;white-space: normal; " +
                                         $"word-spacing: 5px; line-height: 42px; ");
        return firstNode.OuterHtml;
        void ExtractTextFromNode(HtmlNode nodex)
        {
            if (nodex.NodeType == HtmlNodeType.Text)
            {
                string text = nodex.InnerHtml;
                if (!string.IsNullOrEmpty(text))
                {
                    builder.Append(text);
                }
            }
            else if (nodex.NodeType == HtmlNodeType.Element)
            {
                if (nodex.Name == "span")
                {
                    var className = nodex.GetAttribute("class");
                    var innerText = nodex.InnerText;
                    var ff = Extractor.ExtractorFontFamily(className);
                    if (Parameters.InlineWholeFontFamilies.Contains(ff))
                    {
                        innerText = " [" + innerText + "] ";
                        builder.Append(innerText);
                    }
                    else
                    {
                        builder.Append(innerText);
                    }
                }
                else
                {
                    foreach (var childNode in nodex.ChildNodes)
                    {
                        ExtractTextFromNode(childNode);
                    }
                }
            }
        }
    }


}